<?php

namespace app\common;
use think\facade\Config;
use app\model\system\config\SystemConfig;
use app\model\user\UserNotice;
use think\facade\Db;
use think\facade\Validate;
use think\exception\ValidateException;

/**
 * 工具
 * 常用工具类
 * @package app\common\controller
 */
class Tools
{   


    //根据卡号获取银行信息
    public static function get_bank_name($card){
        $list = Config::get('bankList');
        $card_8 = substr($card,0,8);
        if(isset($list[$card_8])){
            return $list[$card_8];
        }
        $card_6 = substr($card,0,6);
        if(isset($list[$card_6])){
            return $list[$card_6];
        }
        $card_5 = substr($card,0,5);
        if(isset($list[$card_5])){
            return $list[$card_5];
        }
        $card_4 = substr($card,0,4);
        if(isset($list[$card_4])){
            return $list[$card_4];
        }
        return false;
    }

    public static function get_config($key){
        
    }

   public static function curl($type, $url, $data=[], $headerFile=false) {
        $type = strtoupper($type);
        $type_list = ['POST', 'GET', 'PUT'];
        if(!in_array($type, $type_list)) $type = 'POST';
        $ch = curl_init();
        // 请求类型
        if($type == 'POST') {
            curl_setopt($ch, CURLOPT_POST, 1);
        }else if($type == 'PUT') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT"); //设置请求方式
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_ENCODING, ''); // 这个是解释gzip内容, 解决获取结果乱码 gzip,deflate
        // 是否存在请求字段信息
        if(!empty($data['data'])) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data['data']);
        }
        // 是否存在cookie
        if(!empty($data['cookie'])) {
            curl_setopt($ch, CURLOPT_COOKIE, $data['cookie']);
        }
        // 请求头
        if(!empty($data['header'])) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $data['header']);
        }

        // 证书
        if(!empty($data['ssl_cert'])) {
            curl_setopt($ch,CURLOPT_SSLCERT, $data['ssl_cert']);
        }
        if(!empty($data['ssl_key'])) {
            curl_setopt($ch,CURLOPT_SSLKEY, $data['ssl_key']);
        }

        // 返回ResponseHeader
        if($headerFile) {
            curl_setopt($ch, CURLOPT_HEADER, 1);
        }
        // 设置请求超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 6);
        // 发送请求
        $result = curl_exec($ch);
        if (curl_errno($ch)) return false;
        curl_close($ch);
        return $result;
    }

    /**
     * @name:生成订单号
     * @author:Sam
     * @date:2024-10-14
     * @param:[string] param [desc]
    */
    public static function create_order_no($table = 'order', $field = 'order_sn', $prefix = 'OD')
    {
        $order_sn = $prefix . date('YmdHis') . rand(10, 99);
        while (1) {
            $count = Db::name("{$table}")->where([$field => $order_sn])->count();
            if ($count <= 0) {
                break;
            }
            $order_sn = $prefix . date('YmdHis') . rand(10, 99);
        }
        return $order_sn;
    }


        /**
     * 快速验证方法
     *
     * @param array $data 待验证的数据
     * @param array $rules 验证规则
     * @param array $messages 自定义错误信息
     * @return bool
     * @throws ValidateException
     */
    public static function check(array $data, array $rules, array $messages = []): bool
    {
        $validate = Validate::rule($rules)->message($messages);
        
        if (!$validate->check($data)) {
            // 抛出异常，包含错误信息
            throw new ValidateException($validate->getError());
        }

        return true;
    }


    // 生成要上传的 COS 文件路径文件名
     public  static  function generateCosKey($ext) {
        $ymd = date('Ymd');
        $r = substr('000000' . rand(), -6);
        $cosKey = 'store/' . date('Y'). '/' . date('m') . '/' . md5($r);
        if ($ext) {
             $cosKey = $cosKey . '.' . $ext;
        }
        return $cosKey;
    }

}
